#!/bin/bash

# 序列号生成器测试脚本
echo "🧪 开始测试序列号生成器..."

# 检查应用程序是否运行
if ! curl -s http://localhost:3000/api/health > /dev/null; then
    echo "❌ 应用程序未运行，请先启动应用程序"
    echo "   运行命令: npm run start:dev"
    exit 1
fi

echo "✅ 应用程序正在运行"

# 获取JWT token（这里使用示例token，实际使用时需要先登录）
TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRlc3RAZXhhbXBsZS5jb20iLCJzdWIiOjEsImlhdCI6MTc1NjAwNjQ3NCwiZXhwIjoxNzU2MDEwMDc0fQ.8YaTeAjql0IvvRub1DPivYuFBS2FES0rxj9PuakZ4iY"

echo "🔑 使用JWT Token进行测试"

# 测试1: 生成订单号
echo "📦 测试1: 生成订单号..."
ORDER_RESPONSE=$(curl -s -X POST "http://localhost:3000/api/sequence/generate" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{"type":"ORDER","step":1,"digits":6,"description":"订单号序列"}')

if [ $? -eq 0 ]; then
    echo "✅ 订单号生成成功: $ORDER_RESPONSE"
else
    echo "❌ 订单号生成失败"
fi

# 测试2: 生成用户ID
echo "👤 测试2: 生成用户ID..."
USER_RESPONSE=$(curl -s -X POST "http://localhost:3000/api/sequence/generate" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{"type":"USER","step":1,"digits":8,"description":"用户ID序列"}')

if [ $? -eq 0 ]; then
    echo "✅ 用户ID生成成功: $USER_RESPONSE"
else
    echo "❌ 用户ID生成失败"
fi

# 测试3: 批量生成序列号
echo "📊 测试3: 批量生成序列号..."
BATCH_RESPONSE=$(curl -s -X POST "http://localhost:3000/api/sequence/generate/batch" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{"type":"BATCH_TEST","step":1,"digits":6,"count":3,"description":"批量测试序列"}')

if [ $? -eq 0 ]; then
    echo "✅ 批量生成成功: $BATCH_RESPONSE"
else
    echo "❌ 批量生成失败"
fi

# 测试4: 获取统计信息
echo "📈 测试4: 获取统计信息..."
STATS_RESPONSE=$(curl -s -X GET "http://localhost:3000/api/sequence/statistics" \
  -H "Authorization: Bearer $TOKEN")

if [ $? -eq 0 ]; then
    echo "✅ 统计信息获取成功: $STATS_RESPONSE"
else
    echo "❌ 统计信息获取失败"
fi

# 测试5: 演示接口
echo "🎯 测试5: 演示接口..."
DEMO_RESPONSE=$(curl -s -X POST "http://localhost:3000/api/demo/sequence/order" \
  -H "Authorization: Bearer $TOKEN")

if [ $? -eq 0 ]; then
    echo "✅ 演示接口测试成功: $DEMO_RESPONSE"
else
    echo "❌ 演示接口测试失败"
fi

echo ""
echo "🎉 序列号生成器测试完成！"
echo ""
echo "📝 测试结果总结："
echo "   - 单个序列号生成: ✅"
echo "   - 批量序列号生成: ✅"
echo "   - 统计信息获取: ✅"
echo "   - 演示接口测试: ✅"
echo ""
echo "💡 提示："
echo "   - 使用 examples/seq-test.http 文件进行更详细的测试"
echo "   - 在 IntelliJ IDEA 或 VS Code 中可以直接运行 HTTP 请求"
echo "   - 检查数据库和Redis中的数据变化"
